package com.xzy.scheduling.service.c;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * 说明：
 * <p>1.使用默认的SingleThreadScheduledExecutor处理定时任务</p>
 * <p>2.将定时任务改为异步执行</p>
 *
 * @author xzy
 * @date 2022/12/8  21:28
 */
@Slf4j
//@Service
//@EnableAsync
//@Configuration
public class MyScheduledService  {

    /**
     * Task1：每隔1秒处理一次，处理时长10秒
     */
    @Async
    @Scheduled(cron = "0/1 * * * * ? ")
    public void task1() {
        log.info("Task1：start");
        aLongTimeTask(10000);
        log.info("Task1：end\n");
    }

    /**
     * Task2：每隔1秒处理一次，处理时长1秒
     */
    @Async
    @Scheduled(cron = "0/1 * * * * ? ")
    public void task2() {
        log.info("Task2：start");
        aLongTimeTask(1000);
        log.info("Task2：end\n");
    }

    private void aLongTimeTask(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
